<!doctype html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>添加好友</title>
    <!-- Set render engine for 360 browser -->
    <meta name="renderer" content="webkit">
    <!-- No Baidu Siteapp-->
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--<meta name="format-detection" content="telephone = no" />-->
    <!-- Add to homescreen for Chrome on Android -->
    <meta name="mobile-web-app-capable" content="yes">
    <!-- Add to homescreen for Safari on iOS -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="添加好友" />
    <meta name="msapplication-TileColor" content="#090a0a">
    <link rel="stylesheet" href="__ROOT__/static/home/css/css_style.css">
    <script src="__ROOT__/static/home/css/userInfo/layer.mobile-v2.0/layer_mobile/layer.js"></script>
    <script src="__ROOT__/static/home/js/jquery-1.11.1.min.js"></script>
    <link href="__ROOT__/static/home/css/userInfo/layer.mobile-v2.0/layer_mobile/need/layer.css" rel="stylesheet"/>
    <link href="__ROOT__/static/home/css/userInfo/css/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="__ROOT__/static/home/css/userInfo/css/style.css" />
    <link rel="stylesheet" href="__ROOT__/static/home/css/userInfo/css/top_nav.css" />

    <script>
        //搜索好友
        $(function(){
            $('#search').click(function(){
                var params={}
                params.friendId=$('.friends').val();

                $.ajax({

                            cache:true,
                            type:'POST',
                            url:"{:url('home/Userinfo/addFriends')}" ,
                            data: params,
                            async:false,
                            success:function(data){
                                if(data==0){
                                    layer.open({
                                        content: '用户不存在'
                                        ,skin: 'msg'
                                        ,time: 2 //2秒后自动关闭
                                    });
                                }else if(data==2){
                                    layer.open({
                                        content: '好友已经存在'
                                        ,skin: 'msg'
                                        ,time: 2 //2秒后自动关闭
                                    });
                                }
                                else{
                                    var friend =JSON.parse(data);
                                    var content= '<dl>'+
                                            '<dt>'
                                            +'<strong>'+friend.user_name+'&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;'
                                            +'<input type="button"  onclick="add()" value="添加" >'
                                            +'<input type="hidden"  class="idF" value="'
                                            +friend.id+'" >'
                                            +'</strong>'
                                            +'</dt>'
                                            + '</dl>';
                                    $('.content').append(content);

                                }
                            }

                        }
                )
            });
        });
        //添加好友
        function add(){
            var params ={};
            params.id=$(".idF").val();
            $.ajax({
                cache:true,
                type:'POST',
                url:"{:url('home/Userinfo/toAddFriend')}" ,
                data: params,
                async:false,
                success:function(data){

                    if(data==1){
                        layer.open({
                            content: '添加成功'
                            ,skin: 'msg'
                            ,time: 1 //2秒后自动关闭
                        });
                       setTimeout("window.location.reload()",1000);
                    }else if(data==0){
                        layer.open({
                            content: '添加失败'
                            ,skin: 'msg'
                            ,time: 2 //2秒后自动关闭
                        });
                        window.location.reload();
                    }
                }

            });

        }
    </script>
</head>
<body>
<div style="height: 50px;background-color: powderblue;">
    <div class="top_nav" style=""><a href="{:url('home/Userinfo/friends')}">< 返回</a></div>
    <div class="top_navC">添加好友</div>
    <div class="top_nav" id="search">搜索</div>
 </div>
    <div class="user-order-box content" style="text-align: center">
        <dl>
            <dt style="width: 100%;height: 100%; text-align: center;line-height: 10px">
                <p>
                    <input type="text" name="friends" class="friends" placeholder="请输入好友Id/好友昵称" >
                </p>
            </dt>
        </dl>
    </div>
</body>
</html>
